<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="account">
            <el-input v-model="loginForm.account" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">
              用户平台使用协议
            </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 350px" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import crypto from 'crypto'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: process.env.NODE_ENV === 'development' ? '15200926763' : '',
        password: process.env.NODE_ENV === 'development' ? '888888' : '',
        isAgree: false
      },
      // 对表单进行规则校验
      loginRules: {
        // 用户名验证
        account: [
          {
            required: true,
            message: '请输入用户账号',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{6,12}$/,
            message: '用户名格式不对',
            trigger: 'blur'
          }
        ],
        // 密码验证
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'

          }

        ],
        // // 用户协议验证
        isAgree: [
          {
            validator: (rule, value, callback) => {
              // rule规则
              // value检查的数据 true/false
              // callback 函数 执行这个函数
              // 成功执行callback 失败也执行callback(错误对象 new Error(错误信息))
              value ? callback() : callback(new Error('没有勾选用户平台协议'))
            }
          }
        ]
      }
    }
  },
  methods: {
    // 点击登录测试点击表单验证
    async login() {
      const isOK = await this.$refs.loginForm.validate()
      const md5 = crypto.createHash('md5')
      const password = md5.update(this.loginForm.password).digest('hex')
      console.log(password)
      this.loginForm.password = password
      if (isOK) {
        try {
          await this.$store.dispatch('user/login', this.loginForm)
          // 登录成功的提示信息
          // this.$message.success('登录成功！')
          setTimeout(() => {
          // 3秒后跳转到首页
            this.$router.push('/')
          }, 3000)
        } catch (error) {
        // 登录失败的提示信息
          this.$message.error('登录失败，请检查用户名和密码！')
        }
      } else {
      // 输入验证不通过的提示信息
        this.$message.warning('请填写完整的用户名和密码！')
      }
    }
  }
}
</script>
<style lang="scss">
.login-container {
  width: 100%;
  display: flex;
  align-items: stretch;
  height: 100%;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../icons/img/1bjt.jpg) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color: #606266;
    }
  }
}
</style>
